﻿using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Raytracer.Generator;
using RaytracerLib;
using RaytracerLib.MathLib;
using RaytracerLib.RenderableObjects;
using RaytracerLib.Volumes;
using RaytracerLib.Samplers;
using System.Diagnostics;

namespace RaytracerTest
{
    [TestClass]
    public class MaterialsTest : TestBase
    {
        [TestMethod]
        public void Materials_Test()
        {
            // Replacing
            {
                Scene scene = Raytracer.Generator.Scenes.Sphere_Earth();

                var red_mat = Raytracer.Generator.TestMaterials.Solid("Red", ColorFloat.SRGB_Red);
                scene.RenderableObjects["sphere"].DefaultMaterial = red_mat;

                Assert.IsTrue(scene.Materials.Count() == 3);
                scene.Materials.RemoveNotUsed();
                Assert.IsTrue(scene.Materials.Count() == 2);
                Assert.IsTrue(scene.Materials["Red"].Equals(red_mat));
                Assert.IsTrue(scene.Materials["Wood"] != null);
            }

            // Replacing with same but changed name.
            {
                Scene scene = Raytracer.Generator.Scenes.Sphere_Earth();

                var sp_mat = scene.RenderableObjects["sphere"].DefaultMaterial.DeepClone();
                sp_mat.Name = "test";

                scene.RenderableObjects["plane"].DefaultMaterial = sp_mat;

                Assert.IsTrue(scene.Materials.Count() == 3);
                scene.Materials.RemoveNotUsed();
                Assert.IsTrue(scene.Materials.Count() == 2);
            }

            // Replacing with diff but same name.
            {
                Scene scene = Raytracer.Generator.Scenes.Sphere_Earth();

                var red_mat = Raytracer.Generator.TestMaterials.Solid("Earth", ColorFloat.SRGB_Red);
                scene.RenderableObjects["sphere"].DefaultMaterial = red_mat;

                Assert.IsTrue(scene.Materials.Count() == 3);
                scene.Materials.RemoveNotUsed();
                Assert.IsTrue(scene.Materials.Count() == 2);
                Assert.IsTrue(scene.Materials["Earth 1"].Equals(red_mat));
                Assert.IsTrue(scene.Materials["Wood"] != null);
            }
        }
    }
}